package com.ctcc.serviceImpl;

import com.ctcc.entity.count.ProductList;
import com.ctcc.entity.count.ProductList_;
import com.ctcc.repository.count.ListRepository;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

/**
 * Created by Peanut on 2018/4/3.
 */
@Service
public class ProductListImpl {
    Log log= LogFactory.getLog(ProductListImpl.class);
    @Autowired
    ListRepository listRepository;

    public  ProductList getProduct(String openId,String productPriceTy,String productName,String productTypeId){
        return  listRepository.findOne(new Specification<ProductList>() {
        @Override
       public Predicate toPredicate(Root<ProductList> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                 return criteriaBuilder.and
                    (criteriaBuilder.equal(root.get(ProductList_.productPriceTy), productPriceTy),
                            criteriaBuilder.equal(root.get(ProductList_.openId), openId),
                          criteriaBuilder.equal(root.get(ProductList_.productName), productName),
                            criteriaBuilder.equal(root.get(ProductList_.productTypeId), productTypeId));
                      }
                }
        );
    }




    //保存清单
    public Boolean saveList(ProductList productList){
        try {
            listRepository.save(productList);
            return true;
        }
        catch (Exception e){
            log.info("add error");
        }
        return false;

    }
    /**
     * 更据openid 获取list
     */
    public List<ProductList> getUserList(String userListOpenId){
        List<ProductList> projectlist = listRepository.findAll(new Specification<ProductList>() {
            @Override
            public Predicate toPredicate(Root<ProductList> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                return criteriaBuilder.and(criteriaBuilder.equal(root.get(ProductList_.openId), userListOpenId));
            }
        });
        if(projectlist!=null);

            return  projectlist;
    }

    public void upDateCKState(Long itemId,String checkState){
        ProductList productList=listRepository.findOne(itemId);
        productList.setCheckState(checkState);
        listRepository.save(productList);
    }
    public void upDateAllState(String openId,String checkState){
        List<ProductList> projectlist = listRepository.findAll(new Specification<ProductList>() {
            @Override
            public Predicate toPredicate(Root<ProductList> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                return criteriaBuilder.and(criteriaBuilder.equal(root.get(ProductList_.openId), openId));
            }
        });
        for (int i=0;i<projectlist.size();i++) {
            projectlist.get(i).setCheckState(checkState);
            listRepository.save(projectlist.get(i));
        }
    }


    public void changeCount(String openId,String discont){
        List<ProductList> projectlist = listRepository.findAll(new Specification<ProductList>() {
            @Override
            public Predicate toPredicate(Root<ProductList> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                return criteriaBuilder.and(criteriaBuilder.equal(root.get(ProductList_.openId), openId));
            }
        });
        for (int i=0;i<projectlist.size();i++) {
            if(!"4".equals(projectlist.get(i).getProductTypeId())){
                projectlist.get(i).setDisCount(Float.parseFloat(discont));
            }
            listRepository.save(projectlist.get(i));
        }
    }


    public void upDateCount(Long ProductId,String count){
        ProductList productList=listRepository.findOne(ProductId);
        productList.setProductCount(count);
        listRepository.save(productList);
    }
    public void deleteById(Long ProductId){
        listRepository.delete(ProductId);

    }


}
